package com.scau.os.fileSystem.directory;

import java.util.List;

import com.scau.os.fileSystem.memory.FAT;
import com.scau.os.fileSystem.other.MyFileSystemException;

public class DirectoryEntry {

    public static final int DIRECTORY_ENTRY_LENGTH = 8;
    public static final String DIRECTORY_TYPE = " ".repeat(Property.MAX_TYPE_LENGTH);
    public static final byte IS_DIRECTORY = 0b000001000;
    public static final byte IS_COMMON_FILE = 0b000000100;
    public static final byte IS_SYSTEM_FILE = 0b000000010;
    public static final byte IS_READABLE_FILE = 0b000000001;

    public static final DirectoryEntry EMPTY_DIRECTORY_ENTRY = new DirectoryEntry(Path.ROOT_PATH.getAbsolutePath(),
            Property.EMPTY_DIRECTORY_ENTRY__PROPERTY);
    public static final Directory ROOT_DIRECTORY = new Directory(Path.ROOT_PATH.getAbsolutePath(),
            Property.ROOT_DIRECTORY_PROPERTY);

    public Path path;
    public Property property;

    protected DirectoryEntry(String path, Property property) {
        this.path = new Path(path);
        this.property = property;
    }

    protected DirectoryEntry(String path) throws Exception {
        this.path = new Path(path);
        List<String> pathNames = this.path.getPathNames();

        DirectoryEntry directoryEntry = DirectoryEntry.ROOT_DIRECTORY;
        List<DirectoryEntry> children = ((Directory) directoryEntry).getChildren();

        for (String name : pathNames) {
            boolean isFound = false;
            for (DirectoryEntry child : children) {
                if (child.property.getName().trim().equals(name.trim()) == true) {
                    if (child.isDirectory()) {
                        directoryEntry = new Directory(child.path.getAbsolutePath(), child.property);
                        children = ((Directory) directoryEntry).getChildren();
                    } else if (child.isFile()) {
                        directoryEntry = new File(child.path.getAbsolutePath(), child.property);
                        children = null;
                    }
                    isFound = true;
                    break;
                }
            }
            if (isFound == false) {
                throw new MyFileSystemException("路径不存在");
            }
        }
        this.property = directoryEntry.property;
    }

    public List<Integer> getBlocks() throws Exception {
        return FAT.INSTANCE.getBlocks(property.getStartBlock());
    }

    public Directory getFatherDirectory() throws Exception {
        return new Directory(path.getAbsolutePath().substring(0, path.getAbsolutePath().lastIndexOf("/")));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((property == null) ? 0 : property.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (obj instanceof DirectoryEntry == false)
            return false;
        DirectoryEntry other = (DirectoryEntry) obj;
        return this.property.equals(other.property);
    }

    public boolean isDirectory() {
        return property.equals(Property.EMPTY_DIRECTORY_ENTRY__PROPERTY) == false
                && property.getType().trim().equals(DIRECTORY_TYPE.trim()) == true;
    }

    public boolean isFile() {
        return property.equals(Property.EMPTY_DIRECTORY_ENTRY__PROPERTY) == false
                && property.getType().trim().equals(DIRECTORY_TYPE.trim()) == false;
    }

    public Property getProperty() {
        return property;
    }

    public void setProperty(Property property) {
        this.property = property;
    }

    public Path getPath() {
        return path;
    }

    public void setPath(Path path) {
        this.path = path;
    }

}